<?php
class Price_rule extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}
	public function index()
	{
		$data = get_initial_data($this);
		$this->load->model('price_rule_model');
		$data['result'] = $this->price_rule_model->get_all();
		$data['title'] = 'new price_rule';
		$this->load->view('templates/header', $data);
		$this->load->view('price_rule/index', $data);
		$this->load->view('templates/footer', $data);
	}
	public function add()
	{
		$data = get_initial_data($this);
		$this->form_validation->set_rules('percent', 'percent',  'trim|required|integer|greater_than[-1]|xss_clean|callback_check_duplicate');
		$this->form_validation->set_rules('discount', 'discount',  'trim|required|xss_clean|integer|greater_than[-1]');
		if ($this->form_validation->run()){
			$this->load->model('price_rule_model');
			$this->price_rule_model->add($this->input->post('percent'),$this->input->post('discount'));
			$data['message'] = Messages::success_create_price_rule;
			$data['title'] = 'new price_rule';
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);
		}else
		{
			$data['title'] = 'new price_rule';
			$this->load->view('templates/header', $data);
			$this->load->view('price_rule/add', $data);
			$this->load->view('templates/footer', $data);
		}
	}

	public function  check_duplicate($str){
		$id = $this->input->post('price_rule_id');
		$this->load->model('price_rule_model');
		if ($id){
			$temp = $this->price_rule_model->get_id($str);
			if ($temp == -1 || $temp = $id){
				return true;
			}
		}else{
			if ($this->price_rule_model->get_id($str) == -1){
				return true;
			}
		}
		$this->form_validation->set_message('check_duplicate',  Messages::duplicate_price_rule);
		return false;
	}

	public function update_price_rule(){
		if ($this->input->post('price_rule_id') == null){
			redirect('price_rule/update');
		}
		$data = get_initial_data($this);
		$this->form_validation->set_rules('percent', 'percent',  'trim|required|integer|greater_than[-1]|xss_clean|callback_check_duplicate');
		$this->form_validation->set_rules('discount', 'discount',  'trim|required|xss_clean|integer|greater_than[-1]');
		$id = $this->input->post('price_rule_id');
		if ($this->form_validation->run()){
			$this->load->model('price_rule_model');
			if( $this->price_rule_model->update($id,$this->input->post('percent'),$this->input->post('discount'))){
				$data['title'] = 'Update price_rule';
				$data['message'] = Messages::success_update_price_rule;
				$this->load->view('templates/header',$data);
				$this->load->view('message',$data);
				$this->load->view('templates/footer',$data);
			}else{

				$data['message'] = Messages::fail_update_price_rule;
				$this->load->view('templates/header',$data);
				$this->load->view('message',$data);
				$this->load->view('templates/footer',$data);
			}
		}else
		{
			$this->load->model('price_rule_model');
			$result = $this->price_rule_model->get_price_rule_by_id($id);
			$data['result'] = $result;
			$data['title'] = 'new price_rule';
			$this->load->view('templates/header', $data);
			$this->load->view('price_rule/update', $data);
			$this->load->view('templates/footer', $data);
		}
	}

	public function delete(){
		$price_rule_id =$this->input->post('price_rule_id');
		if ($price_rule_id){
			$this->load->model('price_rule_model');
			if($this->price_rule_model->delete($price_rule_id)){
				echo 1;
				return;
			}else
				echo 0;
		}else{
			redirect('');
		}
	}

	public function update($id)
	{
		$data = get_initial_data($this);
		if ($id != null){
			$data['title']='Update price_rule';
			$this->load->model('price_rule_model');
			$result = $this->price_rule_model->get_price_rule_by_id($id);
			if ($result != null){
				$data['result'] = $result;
				$this->load->view('templates/header',$data);
				$this->load->view('price_rule/update',$data);
				$this->load->view('templates/footer',$data);
				return;
			}
		}
		$data['message'] = Messages::unable_find_price_rule;
		$this->load->view('templates/header',$data);
		$this->load->view('message',$data);
		$this->load->view('templates/footer',$data);
	}


	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}
}